$(function() {
	// 获取当前页面的URL
	var fullUrl = window.location.href;

	// 提取查询字符串
	var queryString = fullUrl.split("?")[1];

	// 解析查询字符串
	var params = {};
	if (queryString) {
		var pairs = queryString.split('&');
		for (var i = 0; i < pairs.length; i++) {
			var pair = pairs[i].split('=');
			var key = decodeURIComponent(pair[0]);
			var value = decodeURIComponent(pair[1] || '');
			params[key] = value;
		}
	}

	// 访问参数
	var roomTypeId = params.roomId;
	var checkStr = params.check_time;
	var leaveStr = params.leave_time;

	// 将输入的日期字符串转换为Date对象
	var startDate = new Date(checkStr);
	var endDate = new Date(leaveStr);

	// 计算两个日期之间的时间差（毫秒）
	var timeDiff = endDate - startDate;

	// 将时间差转换为天数
	var daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));

	// 创建一个日期数组，包含两个日期之间的所有日期
	var dateArray = [];

	var currentDate = startDate;
	while (currentDate <= endDate) {
		dateArray.push(currentDate.toISOString().split('T')[0]);
		currentDate.setDate(currentDate.getDate() + 1);
	}

	$(".days").html(daysDiff + "晚")

	var checkStrs = checkStr.split('-');
	var leaveStrs = leaveStr.split('-');

	$(".checkMouthDayStr").html(checkStrs[1] + "月" + checkStrs[2] + "日");
	$(".leaveMouthDayStr").html(leaveStrs[1] + "月" + leaveStrs[2] + "日")
	$(".startTime").html(checkStrs[1] + "-" + checkStrs[2] + " 18:00 前")
	$(".endTime").html(checkStrs[1] + "-" + checkStrs[2] + " 18:00 后")

	var hotelId;

	$.ajax({
		url: "http://localhost:8080/userBook/findRoomNum",
		async: false,
		method: "get",
		data: {
			roomId: roomTypeId,
			check_time: checkStr,
			leave_time: leaveStr
		},
		success: function(data) {
			$(".xuanze").empty();
			var roomNum = data.t;
			for (let i = 0; i < roomNum; i++) {
				var raw = `<option value=` + (i + 1) + `>` + (i + 1) + `</option>`
				$(".xuanze").append(raw);
			}

			$.ajax({
				url: "http://localhost:8080/userBook/findByRoomTypeId4Info",
				async: false,
				method: "get",
				data: {
					roomTypeId: roomTypeId,
				},
				success: function(data) {

					var info = data.t;

					hotelId = info.id;

					var list = data.t.list;

					$(".hotelName").html(info.hotel_name);
					$("#fangxing").html(info.room_type);
					$("#dizhi").html(info.hotel_address);

					$(".facility").empty();
					for (var i = 0; i < list.length; i++) {
						var raw = `<div data-v-872ed92c="" class="flex items-center">` +
							list[i]
							.facility_info + `<div data-v-872ed92c=""
													class="line"></div>
											</div>`

						$(".facility").append(raw);
					}

					var fan;
					if (info.have_meal === true) {
						fan = "含早餐";
					} else {
						fan = "不含早餐";
					}

					$(".meal").empty();

					for (var i = 0; i < daysDiff; i++) {

						var raw;
						if (i < (daysDiff - 1)) {
							raw = `<div data-v-340074ea="" class="flex justify-between">
												<div data-v-340074ea="">` + dateArray[i] + fan + ` </div>
												<div data-v-340074ea="" class="every">1 x ¥` + info.price + `</div>
											</div>`
						} else {
							raw = `<div data-v-340074ea="" class="flex justify-between">
												<div data-v-340074ea="">` + dateArray[i] + fan + ` </div><!---->
											</div>`
						}

						$(".meal").append(raw);
					}

					$(".rtPrice").html(daysDiff + '晚￥' + (info.price * daysDiff));
					$(".totalPrice").html('¥' + (info.price * daysDiff));

					$(".xuanze").change(function() {
						var num = $(this).val();

						var m = $(".every").html().split("x");

						$(".every").html(num + 'x' + m[1]);

						var totalPrice = $(".rtPrice").html().split("￥")[1];

						$(".totalPrice").html("¥" + (totalPrice * num))
					});
				}
			});
		}
	});

	var token = sessionStorage.getItem('token');

	$(".robbery").click(function() {

		var num = $(".xuanze").val();
		var price = $(".rtPrice").html().split("￥")[1];
		var phone = $(".ipt2").val();

		if (phone === null || phone === '') {
			alert("请输入手机号")
		} else {
			$.ajax({
				url: "http://localhost:8080/userBook/saveBook",
				method: "post",
				headers: {
					token: token
				},
				data: {
					check_time: checkStr,
					leave_time: leaveStr,
					room_num: num,
					book_price: price,
					phone: phone,
					hotelId: hotelId,
					roomtypeId: roomTypeId
				},
				success: function(data) {
					if (data.t > 0) {
						var totalPrice = $(".rtPrice").html().split("￥")[1];
						window.location.href = 'pay.html?totalPrice=' + totalPrice +
							'&check_time=' + checkStr +
							'&leave_time=' + leaveStr +
							'&rtPrice=' + price +
							'&phone=' + phone +
							'&hotelId=' + hotelId +
							'&daysDiff=' + daysDiff;
					}
				}
			});
		}
	})

});